package com.lanou.moving.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lanou.moving.R;
import com.lanou.moving.entity.MVHotRoot;
import com.lanou.moving.utils.Exchange;
import com.umeng.socialize.utils.ResUtil;

import java.util.List;

import it.sephiroth.android.library.picasso.Picasso;

/**
 * Created by 梁树珍 on 15/11/4.
 */
public class MVListHotAdapter extends BaseAdapter{
    private Context context;
    private List<MVHotRoot.DataEntity> entityList;

    public MVListHotAdapter(Context context, List<MVHotRoot.DataEntity> entityList) {
        this.context = context;
        this.entityList = entityList;
    }

    public void addDatas(List<MVHotRoot.DataEntity> entityList) {
        this.entityList.addAll(entityList);
        notifyDataSetChanged();
    }

    public void refreshDatas(List<MVHotRoot.DataEntity> entityList) {
        this.entityList = entityList;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return entityList == null ? 0 : entityList.size();
    }

    @Override
    public Object getItem(int position) {
        return entityList == null ? null : entityList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.song_list_item, parent, false);
            holder = new ViewHolder();
            holder.song_list_item_img = (ImageView) convertView.findViewById(R.id.song_list_item_img);
            holder.song_list_item_title = (TextView) convertView.findViewById(R.id.song_list_item_title);
            holder.song_list_item_listen_count = (TextView) convertView.findViewById(R.id.song_list_item_listen_count);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        MVHotRoot.DataEntity data = (MVHotRoot.DataEntity) getItem(position);
        if (data.getPickCount() == 0) {
            holder.song_list_item_listen_count.setVisibility(View.GONE);
        } else {
            holder.song_list_item_listen_count.setVisibility(View.VISIBLE);
            String num = Exchange.getCount(data.getPickCount());
            holder.song_list_item_listen_count.setText(num);
            holder.song_list_item_listen_count.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.icon_mv_play_count, 0, 0, 0);
        }
        holder.song_list_item_title.setText(data.getVideoName());
        Picasso.with(context).load(data.getPicUrl()).placeholder(R.mipmap.song_list_before)
                .error(R.mipmap.song_list_before).into(holder.song_list_item_img);
        return convertView;
    }

    private class ViewHolder {
        private ImageView song_list_item_img;
        private TextView song_list_item_title, song_list_item_listen_count;
    }
}
